package cn.stylefeng.guns.modular.weixin.config;

import com.github.binarywang.wxpay.service.*;
import org.springframework.boot.context.properties.*;
import org.springframework.beans.factory.annotation.*;
import com.github.binarywang.wxpay.config.*;
import org.apache.commons.lang3.*;
import com.github.binarywang.wxpay.service.impl.*;
import org.springframework.context.annotation.*;
import org.springframework.boot.autoconfigure.condition.*;

@Configuration
@ConditionalOnClass({ WxPayService.class })
@EnableConfigurationProperties({ WxPayProperties.class })
public class WxPayConfiguration
{
    private WxPayProperties properties;
    
    @Autowired
    public WxPayConfiguration(final WxPayProperties properties) {
        this.properties = properties;
    }
    
    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        final WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
        payConfig.setUseSandboxEnv(false);
        final WxPayService wxPayService = (WxPayService)new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
